package com.tc.cipher;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;

public class ThreeDESUtil {

	private static String DEFAULT_THREE_DESKEY = "g2eJkdkLN4l1wj2S0CbyIINniZHZCzeJ";

	private static Logger log = LoggerFactory.getLogger(ThreeDESUtil.class);
	public static void main(String[] args) {
		try {
			String key = geneThreadDes();
			System.out.println(key);
			String encryptStr = encrypt("892563", key);
			decrypt(encryptStr, key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生产3des秘钥
	 * @return
	 */
	public static String geneThreadDes() {
		try {
			KeyGenerator kg = KeyGenerator.getInstance("desede");
			kg.init(112);
			byte[] keyBytes = kg.generateKey().getEncoded();
			String keyBase64Str = Base64.encodeBase64String(keyBytes);
			System.out.println("desede key:" + keyBase64Str);
			return keyBase64Str;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	public static String decryptDefault(String sdata) {
		return decrypt(sdata,DEFAULT_THREE_DESKEY);
	}
	/**
	 * DES解密处理
	 * @return plaintext
	 */
	public static String decrypt(String sdata, String desKey) {
	    if (StringUtils.isNotBlank(sdata)) {
	        try {
	            byte[] key = Base64.decodeBase64(desKey);
	            byte[] data = Base64.decodeBase64(sdata);
	            Key deskey = null;
	            DESedeKeySpec spec = new DESedeKeySpec(key);
	            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
	            deskey = keyfactory.generateSecret(spec);
	            Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");
	            cipher.init(2, deskey);
	            byte[] bOut = cipher.doFinal(data);
	            String sdata_encode = new String(bOut, "UTF-8");
	            return sdata_encode;
	        } catch (Exception e) {
	            log.error("解密出错", e);
	        }
        }
		return sdata;

	}

	/**
	 * DES加密处理
	 * @return ciphertext
	 */
	public static String encrypt(String sdata, String desKey) {
	    if (StringUtils.isNotBlank(sdata)) {
	        try {
	            byte[] key = Base64.decodeBase64(desKey);
	            byte[] data = sdata.getBytes("UTF-8");
	            Key deskey = null;
	            DESedeKeySpec spec = new DESedeKeySpec(key);
	            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
	            deskey = keyfactory.generateSecret(spec);
	            Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");
	            cipher.init(1, deskey);
	            byte[] bOut = cipher.doFinal(data);
	            return Base64.encodeBase64String(bOut);
	        } catch (Exception e) {
	            log.error("加密出错", e);
	        }
        }
		return sdata;
	}

	public static String encryptDefault(String sdata) {
		return encrypt(sdata,DEFAULT_THREE_DESKEY);
	}
}
